package com.zian.controller.user;


import com.zian.common.R;
import com.zian.domain.dto.UserAddressDTO;
import com.zian.domain.po.SysUserAddress;
import com.zian.service.ISysUserAddressService;
import com.zian.validation.ValidGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Slf4j
@Api(tags = "用户收货地址管理")
@RestController
@RequestMapping("/user/address")
@RequiredArgsConstructor
public class SysUserAddressController {

    private final ISysUserAddressService userAddressService;

    @GetMapping
   @ApiOperation("获取用户收货地址列表")
    public R<List<SysUserAddress>> getUserAddressList() {
        log.info("获取用户收货地址列表");
        List<SysUserAddress> list = userAddressService.getUserAddressList();
        return R.ok(list);
    }

    @DeleteMapping("/{id}")
   @ApiOperation("删除用户收货地址")
    public R deleteUserAddress(@PathVariable Long id) {
        userAddressService.deleteUserAddress(id);
        return R.ok();
    }

    @GetMapping("/{id}")
   @ApiOperation("获取用户收货地址")
    public R<SysUserAddress> getUserAddress(@PathVariable Long id) {
        log.info("根据id查询用户收货地址：{}", id);
        SysUserAddress userAddress = userAddressService.getUserAddress(id);
        return R.ok(userAddress);
    }

    @PutMapping
   @ApiOperation("修改用户收货地址")
    public R updateUserAddress(@RequestBody @Validated UserAddressDTO dto) {
        log.info("修改用户收货地址：{}", dto);
        userAddressService.updateUserAddress(dto);
        return R.ok();
    }

    @PostMapping
   @ApiOperation("新增用户收货地址")
    public R addUserAddress(@RequestBody @Validated(ValidGroup.Crud.Create.class) UserAddressDTO dto) {
        log.info("新增用户收货地址：{}", dto);
        userAddressService.addUserAddress(dto);
        return R.ok();
    }

    @GetMapping("/default")
   @ApiOperation("获取默认地址")
    public R<SysUserAddress> getDefaultAddress() {
        log.info("获取默认地址");
        SysUserAddress vo = userAddressService.getDefaultAddress();
        return R.ok(vo);
    }

}
